# !/usr/bin/python3
# _*_ coding: utf-8 _*_
# @Time     : 2021/9/27 上午10:05
# @Author   : HHR
# ___MSG___ : "公共文件"


def StrOfSize(size):
    """递归实现文件大小转换"""
    def strofsize(integer, remainder, level):
        if integer >= 1024:
            remainder = integer % 1024
            integer //= 1024
            level += 1
            return strofsize(integer, remainder, level)

        else:
            return integer, remainder, level

    units = ["B", "KB", "MB", "GB", "TB", "PB"]
    integer, remainder, level = strofsize(size, 0, 0)
    if level + 1 > len(units):
        level = -1

    return ("{}.{:>03d}{}".format(integer, remainder, units[level]))



def flow_sort(json_dict):
    sort_li = []        # 排序后的结点顺序
    for i in json_dict['lines']:
        from_node = json_dict['lines'][i]['from']
        to_node = json_dict['lines'][i]['to']
        if from_node not in sort_li:
            if to_node not in sort_li:
                sort_li.append(from_node)
                sort_li.append(to_node)
            else:
                for i in sort_li:
                    if i == to_node:
                        ind = sort_li.index(i)
                        sort_li.insert(ind, from_node)
                        break
        else:
            if to_node not in sort_li:
                for i in sort_li:
                    if i == from_node:
                        ind = sort_li.index(i)
                        sort_li.insert(ind + 1, to_node)
                        break
            else:
                print('这个是开始结点在里面，结束结点也在里面的情况，不用处理')

    name_li = []
    for i in sort_li:
        a = json_dict['nodes'][i]['name']
        name_li.append(a)
    print(name_li)
    return name_li



def sort_flow(flow_data):
    flow_dict = {}
    for i in flow_data['lines']:
        from_node = flow_data['lines'][i]['from']
        to_node = flow_data['lines'][i]['to']
        if from_node in flow_dict:
            flow_dict[from_node].append(to_node)
        else:
            flow_dict[from_node] = [to_node]

    new_flow_dict = {}
    for item in flow_dict:
        node_name = flow_data['nodes'][item]['name']
        print(node_name)

    # print(flow_dict)






if __name__ == '__main__':
    import json, os

